package com.StarStudio.Controller;

import com.StarStudio.Service.GoodsEvaluationService;
import com.StarStudio.Utils.QueryPageUtils;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.Entity.*;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goodsEvaluation")
@Api(tags = "商品评价接口")
public class GoodsEvaluationController {

    @Autowired
    GoodsEvaluationService goodsEvaluationService;

    /**
     * 新增商品评价接口
     *
     * @param goodsEvaluation 商品评价对象
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/21
     */
    @PostMapping
    @ApiOperation("新增商品评价接口 -> 商品ID、商家ID、用户ID不能为空")
    public Result addEvaluation(@RequestBody GoodsEvaluation goodsEvaluation) {
        if (goodsEvaluation.getGoodsId() == null || goodsEvaluation.getStoreId() == null ||
            goodsEvaluation.getUserId() == null || StringUtils.isBlank(goodsEvaluation.getGoodsEvaluation())) {
            return new Result(HttpStatusCode.BAD_REQUEST, MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        try {
            goodsEvaluationService.insertGoodsEvaluation(goodsEvaluation);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ADD_GOODS_EVALUATION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ADD_GOODS_EVALUATION_SUCCESS);
    }

    /**
     * 修改商品评价接口
     * @param goodsEvaluation 商品评价对象
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/21
     */
    @PutMapping
    @ApiOperation("修改商品评价接口")
    public Result updateEvaluation(@RequestBody GoodsEvaluation goodsEvaluation) {
        if (goodsEvaluation.getGoodsId() == null || goodsEvaluation.getStoreId() == null ||
                goodsEvaluation.getId() == null) {
            return new Result(HttpStatusCode.BAD_REQUEST, MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        try {
            goodsEvaluationService.updateGoodsEvaluation(goodsEvaluation);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.UPDATE_GOODS_EVALUATION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.UPDATE_GOODS_EVALUATION_SUCCESS);
    }

    /**
     * 查询商品评价接口
     * @param goodsEvaluation 商品评价对象
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/21
     */
    @GetMapping
    @ApiOperation("查询商品评价接口")
    public PageResult selectEvaluation(GoodsEvaluation goodsEvaluation) {
        QueryPageUtils.startPage();
        List<GoodsEvaluation> goodsEvaluations;
        PageInfo pageInfo;
        long allRecordNumber;
        try {
            goodsEvaluations = goodsEvaluationService.selectGoodsEvaluation(goodsEvaluation);
            SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
            if (sysUser.isAdmin()){
                allRecordNumber = goodsEvaluationService.findAllRecordNumber();
            }else {
                allRecordNumber = goodsEvaluationService.findStoreAllRecordNumber(sysUser.getUserId());
            }
            pageInfo = new PageInfo<>(goodsEvaluations);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR,MessageConstant.GET_GOODS_EVALUATION_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.GET_GOODS_EVALUATION_SUCCESS, pageInfo.getTotal(), goodsEvaluations, allRecordNumber);
    }

    /**
     * 删除商品评价接口
     * @param ids 记录ID数组
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/21
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除商品评价")
    public Result deleteEvaluation(@PathVariable("ids") Long[] ids) {
        if (ids == null || ids.length == 0){
            return new Result(HttpStatusCode.BAD_REQUEST,MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        try {
            for (Long id : ids) {
                goodsEvaluationService.deleteGoodsEvaluation(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.DELETE_GOODS_EVALUATION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.DELETE_GOODS_EVALUATION_SUCCESS);
    }

    /**
     * 展示本商店最近新增的3条评论
     * @param storeId 商家ID
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/22
     */
    @GetMapping("/storeRecentlyEvaluation")
    @ApiOperation("展示本商店最近新增的3条评论 -> 商家ID不能为空")
    public Result getStoreRecentlyEvaluation(Long storeId){
        if (storeId == null){
            return new Result(HttpStatusCode.BAD_REQUEST,MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        List<GoodsEvaluation> goodsEvaluations;
        try {
            goodsEvaluations = goodsEvaluationService.selectStoreRecentlyEvaluation(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_RECENTLY_GOODS_EVALUATION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.GET_RECENTLY_GOODS_EVALUATION_SUCCESS,goodsEvaluations);
    }

    /**
     * 展示最近新增的3条评论，银行端和商家端共同使用该接口
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/22
     */
    @GetMapping("/recentlyEvaluation")
    @ApiOperation("展示整个商城最近新增的3条评论，银行端和商家端共同使用该接口")
    public Result getRecentlyEvaluation(){
        List<GoodsEvaluation> goodsEvaluations;
        try {
            SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
            if (sysUser == null){
                return new Result(HttpStatusCode.ERROR,MessageConstant.USER_NO_EXIST);
            }
            if (sysUser.isAdmin()){
                goodsEvaluations = goodsEvaluationService.selectRecentlyEvaluation();
            }else {
                goodsEvaluations = goodsEvaluationService.selectStoreRecentlyEvaluation(sysUser.getUserId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_RECENTLY_GOODS_EVALUATION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.GET_RECENTLY_GOODS_EVALUATION_SUCCESS,goodsEvaluations);
    }

    /**
     * 由商品ID获取商品评价
     * @param goodsId 商品ID
     * @return 结果对象
     * @Author ChengLee
     * @Date 2023/05/06
     */
    @GetMapping("/evaluationByGoodsId")
    @ApiOperation("由商品ID获取商品评价 -> 商品ID不能为空")
    public Result getGoodsEvaluationByGoodsId(Long goodsId){
        if (goodsId == null){
            return new Result(HttpStatusCode.BAD_REQUEST,MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        List<GoodsEvaluation> goodsEvaluationList;
        try {
            goodsEvaluationList = goodsEvaluationService.getGoodsEvaluationByGoodsId(goodsId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_GOODS_EVALUATION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.GET_GOODS_EVALUATION_SUCCESS,goodsEvaluationList);
    }
}
